#Set up

#s1
rm(list=ls())

setwd ("C:/Users/cecidy/Google Drive/ZH_analysis/")

source("ZHfunctions.R")
options(scipen=9999)

ZeroHunger <- read.csv("Data/ZHdf/ZeroHungerDF_2020.csv",header=TRUE, sep=",")

#s2
###############################################################################################################

#start1_

#BF and MPI00 robustness sample

##############################################################################################################

#Make a nonBF ZHvariable!

summary(ZeroHunger$BFPerCapTotal00to10for2000Analysis1000)
summary(ZeroHunger$ZHPerCapTotal00to10for2000Analysis1000)

ZeroHunger$ZHPerCapTotal00to10for2000Analysis1000NOBF <- with(ZeroHunger,ZHPerCapTotal00to10for2000Analysis1000-BFPerCapTotal00to10for2000Analysis1000)

#######################################################################################################################

#1. Prepare each dataframe 

#Get Rural municipalities, dont include those very large municipalities
Poverty04 <- subset(ZeroHunger, PopDensity20002000Analysis<=150 & SizeKm22000Analysis<=10000)

load(file = "Data/ForSamples/MPI00list.rda")
load(file = "Data/ForSamples/Basic00.rda")

RuralPoverty04 <- subset(Poverty04, select=c("BFPerCapTotal00to10for2000Analysis1000","ZHPerCapTotal00to10for2000Analysis1000NOBF",Basic00,MPI00List))

RuralPoverty04 <- na.omit(RuralPoverty04)

load(file = "Data/ForSamples/EcludeForRCProb00.rda")
RuralPoverty04 <- subset(RuralPoverty04,(!(IBGECode7digit %in% EcludeForRCProb00)))

RuralPoverty04quality <- RuralPoverty04

#############################################################################################

#Make MPI with the correct 0s
test <- NrOfZeroALL(RuralPoverty04quality)#This gives me all the vars with 0s
MPIadd <- test[[1]]
Otheradd <- test[[2]]

#For the MPI because Ive not previously renamed when adding I just override the vars with 0 with the minimum/2
WithAdded <- lapply(MPIadd,function(y) AddConstant(RuralPoverty04quality,y,addName=""))
WithAdded <- data.frame(do.call("cbind",WithAdded))

RuralPoverty04quality[,MPIadd] <- WithAdded[,MPIadd]#just override

#Then make the MPI - use the 00 function
RuralPoverty04quality <- MakeMPI00(RuralPoverty04quality)

#Make the no Ceros
RuralPoverty04quality <- cbind(RuralPoverty04quality,lapply(Otheradd,function(y) AddConstant(RuralPoverty04quality,y,addName="nocero")))#

#Add log
OtheraddCero <- paste(Otheradd,"nocero",sep="")#here which are names no cero

cols.log <- c("BFPerCapTotal00to10for2000Analysis1000","ZHPerCapTotal00to10for2000Analysis1000NOBF","MPI2000for2000Analysis","MPI2010for2000Analysis",
              "GDPPerCapPublicrealN2000for2000Analysis1000",OtheraddCero,"RemoteMinPopFifty2000Analysis",
              "PopDensity20002000Analysis","SizeKm22000Analysis","MeanSlopefor2000analysis","MeanElevationfor2000analysis")
addlog <- function(x) log10(x)
Log10variables <- data.frame(sapply(RuralPoverty04quality[cols.log],addlog))
colnames(Log10variables) <- paste(colnames(Log10variables), "log10",sep="")

#Get all onto ZH
RuralPoverty04quality <- cbind(RuralPoverty04quality,Log10variables)

###############################################################################################################

#Take away the redundandt
Log10variables <- NULL
Poverty04 <- NULL
WithAdded <- NULL
ZeroHunger <- NULL

#############################################################################################

RuralPoverty04quality<- getDataFrame(RuralPoverty04quality,"contr.Sum",
                                     contrastVar1="stateName",contrastVar2="Biomefor2000Analysis",setRefToMostCommonLevel,
                                     BaseVar="BFPerCapTotal00to10for2000Analysis1000log10",
                                     MainName="BFPerCapTotal00to10for2000Analysis1000log10scaledMain")

#fin1_

##############################################################################################################

#start2_

#Prepare vectors of variables for regressions

###############################################################################################################

Depvar <- "MPI2010for2000Analysislog10"
DepVarLog <- "MPI2010for2000Analysislog10"

IndepVarLin <- "BFPerCapTotal00to10for2000Analysis1000log10scaledMain"#already scaled so dont need to do it again

intLin <- c("BFPerCapTotal00to10for2000Analysis1000log10scaledMain","stateName")
intLin <- paste(intLin,collapse="*")

baselineDepVar <- "scale(MPI2000for2000Analysislog10)"

stateName <- "stateName"

xvars <- c("scale(GDPPerCapPublicrealN2000for2000Analysis1000log10)",
           "scale(TotalKcal2000percapperdaynocerolog10)",
           "scale(TotalHectareCrops2000for2000Analysisnocerolog10)","scale(TotalHectarePasture2006for2000Analysisnocerolog10)",
           "scale(TotalHectareFarmsLess502006for2000Analysisnocerolog10)","scale(RemoteMinPopFifty2000Analysislog10)",
           "scale(TotRCpcNOPRONAF00to10for2000Analysis1000nocerolog10)",
           "scale(ChangeCummulativeSPEI98and00to08and10for2000Analysis)",
           "scale(MeanElevationfor2000analysislog10)","scale(MeanSlopefor2000analysislog10)",
           "scale(PopDensity20002000Analysislog10)","scale(SizeKm22000Analysislog10)","scale(ZHPerCapTotal00to10for2000Analysis1000NOBFlog10)",
           "Biomefor2000Analysis")

cbpsDepVar <- "BFPerCapTotal00to10for2000Analysis1000log10"#Keep this logged because I call the non-logged non-logged below!

cbpsbaselineDepVar <- "MPI2000for2000Analysislog10"

cbpsVars <- c("GDPPerCapPublicrealN2000for2000Analysis1000log10","TotalKcal2000percapperdaynocerolog10",
              "TotalHectareCrops2000for2000Analysisnocerolog10","TotalHectarePasture2006for2000Analysisnocerolog10",
              "TotalHectareFarmsLess502006for2000Analysisnocerolog10","RemoteMinPopFifty2000Analysislog10",
              "TotRCpcNOPRONAF00to10for2000Analysis1000nocerolog10",
              "ChangeCummulativeSPEI98and00to08and10for2000Analysis",
              "MeanElevationfor2000analysislog10","MeanSlopefor2000analysislog10",
              "PopDensity20002000Analysislog10","SizeKm22000Analysislog10","ZHPerCapTotal00to10for2000Analysis1000NOBFlog10",
              "stateName","Biomefor2000Analysis")

##########################################################################################################################

#fin2_

###############################################################################################################################

